import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydan -o ydan.json
https://jingpinbd-006.jingpyn.sbs/
'''


class YdanSpider(scrapy.Spider):
    name = "ydan"
    # 前缀
    prefix = 'https://jingpinbd-006.'
    # 中缀
    website = 'jingpyn'
    # 后缀
    suffix = '.sbs/'
    allowed_domains = [website + '.sbs']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        p_url = response.xpath("//div[@class='vod_box']//a//div/@ data-src").extract()
        tags = response.xpath("//div[@class='vod_box']//a//div//span/text()").extract()
        name = response.xpath("//div[@class='vod_box']//a/@ title").extract()
        url = response.xpath("//div[@class='vod_box']//a/@ href").extract()
        if len(url) and len(p_url) and len(name) and len(tags):
            for i in range(len(url)):
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags=tags[i], url=split_joint(self.prefix + self.website + self.suffix, url[i]), name=name[i], pUrl=p_url[i], vUrl="")
        if len(video_url):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags='', url=response.url, name='', pUrl='', vUrl=video_url[0])
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/') and url.endswith('.html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
